<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Exceptions\InvalidRequestException;
use App\Models\Project;
use App\Models\ProjectTeam;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ProjectService
{
    public function get($title)
    {
        $sql = Project::when($title, function ($query, $title) {
            return $query->where(function($query) use ($title) {
                $query->where('title','like', '%'.$title.'%');
            });
        });
        return $sql;
    }

    public function insert($input,$teams){
        if($input['start_date']){
            $input['start_date'] = Date("Y-m-d",$input['start_date'] / 1000);
        }
        if($input['end_date']){
            $input['end_date'] = Date("Y-m-d",$input['end_date'] / 1000);
        }
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }

        //判断项目编号/立项编号是否存在
        if($res['internal_contract_number']){
            $is_internal_contract_number = Project::where('internal_contract_number',$res['internal_contract_number'])->first();
            if($is_internal_contract_number){
                throw new InvalidRequestException('内部合同编号已存在');
            }
        }

        //判断项目编号/立项编号是否存在
        if($res['contract_number']){
            $is_contract_number = Project::where('contract_number',$res['contract_number'])->first();
            if($is_contract_number){
                throw new InvalidRequestException('合同编号已存在');
            }
        }

        //判断项目编号/立项编号是否存在
        if($res['number']){
            $is_number = Project::where('number',$res['number'])->first();
            if($is_number){
                throw new InvalidRequestException('项目编号已存在');
            }
        }

        if($res['item_number']){
            $is_item_number = Project::where('item_number',$res['item_number'])->first();
            if($is_item_number){
                throw new InvalidRequestException('立项编号已存在');
            }
        }

        try {
            $data = Project::create($input);
            return $data;

        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function update($input,$teams){
        if($input['start_date']){
            $input['start_date'] = Date("Y-m-d",$input['start_date'] / 1000);
        }
        if($input['end_date']){
            $input['end_date'] = Date("Y-m-d",$input['end_date'] / 1000);
        }
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }

        //判断项目编号/立项编号是否存在
        if($res['number']){
            $is_number = Project::where('number',$res['number'])->where('id','<>',$res['id'])->first();
            if($is_number){
                throw new InvalidRequestException('项目编号已存在');
            }
        }

        if($res['item_number']){
            $is_item_number = Project::where('item_number',$res['item_number'])->where('id','<>',$res['id'])->first();
            if($is_item_number){
                throw new InvalidRequestException('立项编号已存在');
            }
        }
        unset($res['id']);
        try {
            Project::where('id',$input['id'])->update($res);
            return $input['id'];

        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }



    public function insertTeam($input){
        if ($input['in_time']) {
            $input['in_time'] = Date("H:i:s", $input['in_time'] / 1000);
        }
        if ($input['out_time']) {
            $input['out_time'] = Date("H:i:s", $input['out_time'] / 1000);
        }
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }

        if($res['team_id']){
            $is = ProjectTeam::where('team_id',$res['team_id'])->first();
            if($is){
                throw new InvalidRequestException('该班组已存在');
            }
        }
        try {
            $data = ProjectTeam::create($input);
            return $data;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function updateTeam($input){
        if ($input['in_time']) {
            $input['in_time'] = Date("H:i:s", $input['in_time'] / 1000);
        }
        if ($input['out_time']) {
            $input['out_time'] = Date("H:i:s", $input['out_time'] / 1000);
        }
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }

        unset($res['id']);
        // try {
            ProjectTeam::where('id',$input['id'])->update($res);
            return $input['id'];
        // } catch (\Throwable $th) {
        //     Log::info(json_encode($th));
        //     throw new InternalException('提交失败');
        // }
    }

}